package com.briup.corejava.day19;

import java.io.*;

/**
 * 验证缓冲流效率提升
 * 先用文件流做简单复制，记录时间
 * 再用缓冲流包装节点流，对比时间
 */
public class Test3 {
    public static void main(String[] args) {
        // 三分钟时间，将桌面上的一个大文件使用字节流拷贝到day19/file里
        FileInputStream fis = null;
        FileOutputStream fos = null;
        // 使用缓冲流
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            fis = new FileInputStream("/Users/mastercgx/Desktop/Java基础-day20-2.mov");
            fos = new FileOutputStream("src/com/briup/corejava/day19/file/a.mov");
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            long start = System.currentTimeMillis();
            byte[] b = new byte[1024];
            int len = -1;
            while ((len = bis.read(b))!=-1){
                bos.write(b,0,len);
            }
            long end = System.currentTimeMillis();
            System.out.println("文件复制成功，共耗时："+(end-start)+"毫秒");

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(fos!=null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(fis!=null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
